package com.supermarket.controller;

import com.supermarket.annotation.OperationLogAnnotation;
import com.supermarket.dto.OrderPageQueryDto;
import com.supermarket.entity.Order;
import com.supermarket.service.OrderService;
import com.supermarket.utils.PageResult;
import com.supermarket.utils.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "订单")
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 分页查询订单列表
     */
    @GetMapping("/list")
    @OperationLogAnnotation(operation = "查询订单列表", operationType = "查询")
    public Result<PageResult> orderList(OrderPageQueryDto orderPageQueryDto) {
        log.info("分页查询订单列表：{}", orderPageQueryDto);
        PageResult pageResult = orderService.list(orderPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    @OperationLogAnnotation(operation = "根据id查询订单", operationType = "查询")
    public Result<Order> getById(@PathVariable Long id) {
        log.info("根据id查询订单：{}", id);
        Order order = orderService.getById(id);
        return Result.success(order);
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/status/{status}/{id}")
    @OperationLogAnnotation(operation = "更新订单状态", operationType = "更新")
    public Result<String> updateStatus(@PathVariable Integer status, @PathVariable Long id) {
        log.info("更新订单状态：status={}, id={}", status, id);
        orderService.update(id, status);
        return Result.success();
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    @OperationLogAnnotation(operation = "删除订单", operationType = "删除")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除订单：{}", id);
        orderService.delete(id);
        return Result.success();
    }
}
